/***********************************************
 This file is part of redia-library-app-ios.
 
 Copyright (c) 2012, 2013 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */


#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <sqlite3.h>
#import "UrlCacheEntry.h"
#import "ProgressDisplayDelegate.h"

@interface UrlCacheManager : NSObject {
	NSString *databasePath;
	NSString *cacheFilesPath;
	
	int cacheFileExpireTimeSeconds;

	sqlite3	*cacheDB;
	BOOL isOffLineMode;
}

@property (nonatomic, copy) NSString *databasePath;
@property (nonatomic, copy) NSString *cacheFilesPath;
@property (nonatomic) BOOL isOffLineMode;
@property (nonatomic) int cacheFileExpireTimeSeconds;

+ (UrlCacheManager*) instance;

- (void)setupDB;

- (void) saveImage:(NSData*)imageData fromUrl:(NSURL*)url imageWidth:(int)width imageHeight:(int)height;

- (UrlCacheEntry*) loadUrl:(NSURL*)url delegate:(id<ImageLoadedNotificationDelegate>)delegate;
- (UrlCacheEntry*) loadUrl:(NSURL*)url delegate:(id<ImageLoadedNotificationDelegate>)delegate doDownloadOnly:(BOOL)download_only DBAlreadyOpen:(BOOL)db_already_open;

- (NSString*) getLocalCacheFilePath;
- (NSURL*) getLocalCacheFileBaseUrl;
- (NSString*) getLocalCacheFileNameForUrl:(NSURL*)url;
- (NSString *)getLocalCacheFileNameForUrl:(NSURL *)url size:(CGSize*)out_size;
- (NSString *)getLocalCacheFileNameForUrl:(NSURL *)url size:(CGSize*)out_size initiateDownload:(BOOL)do_download;

- (void) removeCacheFile:(NSString*) filename;
- (void) removeEntry:(sqlite_int64)row_id; 

- (BOOL)checkFilenameInDB:(NSString*)filename;
- (void)checkConsistency;
- (void)checkConsistencyWithProgressDelegate:(id<ProgressDisplayDelegate>)_delegate;

- (void)clearEntireCacheDatabase;

- (bool)openCacheDb;
- (void)closeCacheDb;

@end
